﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace ThoiTrangT2B
{
    public partial class XemSanPham : System.Web.UI.Page
    {
        private static int cID;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadFeature();
                if (Request.QueryString["cID"] != null)
                {
                    cID = Convert.ToInt32(Request.QueryString["cID"]);
                    hplTitle.Text = ">>  " + GetMenuName(cID);
                }

            }
            LoadSanPham("");
        }

        protected void LoadSanPham()
        {

            string category = SqlHelper.ExecuteScalar(CommandType.Text, "SELECT Ten FROM dbo.tblT2BMenu WHERE ID = " + cID).ToString();
            string sSql = "SELECT s.Image,d.TenDM AS Hang,s.TenSP,s.ID,s.Gia,(s.Gia - (s.Gia*s.KM/100)) AS Giacu,s.KM FROM dbo.tblT2BSanPham s " +
                          "INNER JOIN dbo.tblT2BDanhMuc d ON s.ThuongHieuID_DM = d.ID WHERE s.Menu_ID IN (" + MyCommon.GetSubItems(cID) + ") ORDER BY s.UpdateDate DESC";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sSql);
            string Img;
            string hang;
            string ten;
            int pID;
            string gia;
            string giacu;
            string km;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UCProductBlockView Lists = (UCProductBlockView)LoadControl("UCProductBlockView.ascx");
                Img = dt.Rows[i]["Image"].ToString();
                hang = dt.Rows[i]["Hang"].ToString();
                ten = dt.Rows[i]["TenSP"].ToString();
                pID = Convert.ToInt32(dt.Rows[i]["ID"].ToString());
                gia = dt.Rows[i]["Gia"].ToString();
                giacu = dt.Rows[i]["Giacu"].ToString();
                km = dt.Rows[i]["KM"].ToString();
                Lists.setValue(pID, Img, hang, ten, gia, giacu, km);
                ProductListHolder.Controls.Add(Lists);
            }
        }

        protected void LoadSanPham(string Expression)
        {
            ProductListHolder.Controls.Clear();
            string category = SqlHelper.ExecuteScalar(CommandType.Text, "SELECT Ten FROM dbo.tblT2BMenu WHERE ID = " + cID).ToString();
            string sSql = "SELECT s.Image,d.TenDM AS Hang,s.TenSP,s.ID,s.Gia,(s.Gia - (s.Gia*s.KM/100)) AS Giacu,s.KM FROM dbo.tblT2BSanPham s " +
                          "INNER JOIN dbo.tblT2BDanhMuc d ON s.ThuongHieuID_DM = d.ID LEFT JOIN dbo.tblT2BKhoHang k " +
                          "ON s.ID = k.SanPhamID WHERE s.Menu_ID IN (" + MyCommon.GetSubItems(cID) + ") " + Expression +
                          " GROUP BY s.Image,d.TenDM,s.TenSP,s.ID,s.Gia,s.KM";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sSql);
            string Img;
            string hang;
            string ten;
            int pID;
            string gia;
            string giacu;
            string km;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UCProductBlockView Lists = (UCProductBlockView)LoadControl("UCProductBlockView.ascx");
                Img = dt.Rows[i]["Image"].ToString();
                hang = dt.Rows[i]["Hang"].ToString();
                ten = dt.Rows[i]["TenSP"].ToString();
                pID = Convert.ToInt32(dt.Rows[i]["ID"].ToString());
                gia = dt.Rows[i]["Gia"].ToString();
                giacu = dt.Rows[i]["Giacu"].ToString();
                km = dt.Rows[i]["KM"].ToString();
                Lists.setValue(pID, Img, hang, ten, gia, giacu, km);
                ProductListHolder.Controls.Add(Lists);
            }
        }

        protected string GetMenuName(int ID)
        {
            return SqlHelper.ExecuteScalar(CommandType.Text, "Select Ten from tblT2BMenu where ID = " + ID).ToString();
        }

        protected void LoadFeature()
        {
            LoadHangSX();
            LoadKhuyenMai();
            LoadKichCo();
            LoadMauSac();
        }

        private void LoadMauSac()
        {
            string sql = "SELECT * FROM dbo.tblT2BDanhMuc WHERE MaDM = 'cl'";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            chkMauSac.DataSource = dt;
            chkMauSac.DataBind();
        }

        private void LoadKichCo()
        {
            string sql = "SELECT * FROM dbo.tblT2BDanhMuc WHERE MaDM = 'size'";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                chkSize.Items.Add(new ListItem("Size " + dt.Rows[i]["TenDM"].ToString(), dt.Rows[i]["ID"].ToString()));
            }
        }

        private void LoadKhuyenMai()
        {
            chkKhuyenMai.Items.Add(new ListItem("0% - 20%", "2"));
            chkKhuyenMai.Items.Add(new ListItem("20% - 40%", "4"));
            chkKhuyenMai.Items.Add(new ListItem("40% - 60%", "6"));
            chkKhuyenMai.Items.Add(new ListItem("60% - 80%", "8"));
            chkKhuyenMai.Items.Add(new ListItem("80% - 100%", "10"));
        }

        private void LoadHangSX()
        {
            string sql = "SELECT * FROM dbo.tblT2BDanhMuc WHERE MaDM = 'hang'";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            chkHangSX.DataSource = dt;
            chkHangSX.DataTextField = "TenDM";
            chkHangSX.DataValueField = "ID";
            chkHangSX.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string Expression = "";
            double giatu, giaden;
            giatu = Convert.ToDouble(pFrom.Text.Trim().Equals("") ? "0" : pFrom.Text.Replace(".",""));
            giaden = Convert.ToDouble(pTo.Text.Trim().Equals("") ? Int32.MaxValue.ToString() : pTo.Text.Replace(".", ""));
            Expression += "AND (s.Gia - (s.Gia*s.KM/100)) > " + giatu + " AND (s.Gia - (s.Gia*s.KM/100)) <= " + giaden + " ";
            string lstHang = "";
            foreach (ListItem item in chkHangSX.Items)
            {
                if (item.Selected)
                {
                    lstHang += item.Value.ToString() + ",";
                }
            }
            if (!lstHang.Trim().Equals(""))
            {
                Expression += " AND ThuongHieuID_DM IN (" + lstHang.Substring(0, lstHang.Length - 1) + ") ";
            }
            if (chkKhuyenMai.SelectedItem != null)
            {
                int KM = Convert.ToInt32(chkKhuyenMai.SelectedValue);
                int from = KM * 10 - 20;
                int to = KM * 10;
                Expression += "AND KM >= " + from + " AND KM <" + to + " ";
            }
            string lstSize = "";
            foreach (ListItem item in chkSize.Items)
            {
                if (item.Selected)
                {
                    lstSize += item.Value.ToString() + ",";
                }
            }
            if (!lstSize.Trim().Equals(""))
            {
                Expression += " AND SizeID_DM IN (" + lstSize.Substring(0, lstSize.Length - 1) + ") ";
            }
            string lstColor = "";
            foreach (RepeaterItem li in chkMauSac.Items)
            {
                CheckBox chkColor = (CheckBox)li.FindControl("chkColor");
                HiddenField hdnID = (HiddenField)li.FindControl("hdnID");
                if (chkColor.Checked)
                {
                    lstColor += hdnID.Value + ",";
                }
            }
            if (!lstColor.Trim().Equals(""))
            {
                Expression += " AND ColorID_DM IN (" + lstColor.Substring(0, lstColor.Length - 1) + ")";
            }
            LoadSanPham(Expression);
        }
    }
}
